/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.ad.domain.Test;
import cn.ibizlab.core.ad.service.TestService;
import cn.ibizlab.core.ad.filter.TestSearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[Test] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractTestResource {

    @Autowired
    public TestService testService;

    @Autowired
    @Lazy
    public TestDTOMapping testDtoMapping;

    @Autowired
    @Lazy
    public TestFilterDTOMapping testFilterDtoMapping;


    /**
    * 创建Create 测试实体
    * 
    *
    * @param dto dto
    * @return ResponseEntity<TestDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"测试实体" },  notes = "Test-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-Create-all') or hasPermission(this.testDtoMapping.toDomain(#dto),'uaa-Test-Create')")
    @PostMapping(value = {"tests","tests"})
    public ResponseEntity<TestDTO> create
            (@Validated @RequestBody TestDTO dto) {
        Test domain = testDtoMapping.toDomain(dto);
        testService.create(domain);
        Test rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(testDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 测试实体
    * 
    *
    * @param id id
    * @return ResponseEntity<TestDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"测试实体" },  notes = "Test-Get ")
    @PostAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-Get-all') or hasPermission(this.testDtoMapping.toDomain(returnObject.body),'uaa-Test-Get')")
    @GetMapping("tests/{id}")
    public ResponseEntity<TestDTO> getById
            (@PathVariable("id") String id) {
        Test rt = testService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(testDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 测试实体
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"测试实体" },  notes = "Test-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-Remove-all') or hasPermission(this.testService.get(#id),'uaa-Test-Remove')")
    @DeleteMapping("tests/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = testService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 测试实体
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<TestDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"测试实体" },  notes = "Test-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-Update-all') or hasPermission(this.testService.get(#id),'uaa-Test-Update')")
    @VersionCheck(entity = "test" , versionfield = "updateTime")
    @PutMapping("tests/{id}")
    public ResponseEntity<TestDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody TestDTO dto) {
        Test domain = testDtoMapping.toDomain(dto);
        domain.setId(id);
        testService.update(domain);
        Test rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(testDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 测试实体
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"测试实体" },  notes = "Test-CheckKey ")
    @PostMapping("tests/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody TestDTO dto) {
        Test domain = testDtoMapping.toDomain(dto);
        Integer rt = testService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 测试实体
    * 
    *
    * @param dto dto
    * @return ResponseEntity<TestDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"测试实体" },  notes = "Test-GetDraft ")
    @GetMapping("tests/getdraft")
    public ResponseEntity<TestDTO> getDraft
            (@SpringQueryMap TestDTO dto) {
        Test domain = testDtoMapping.toDomain(dto);
        Test rt = testService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(testDtoMapping.toDto(rt));
    }

    /**
    * PrepareParam 测试实体
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<TestDTO>
    */
    @ApiOperation(value = "PrepareParam", tags = {"测试实体" },  notes = "Test-PrepareParam ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-PrepareParam-all') or hasPermission(this.testDtoMapping.toDomain(#dto),'uaa-Test-PrepareParam')")
    @PostMapping("tests/{id}/prepareparam")
    public ResponseEntity<TestDTO> prepareParamById
            (@PathVariable("id") String id, @Validated @RequestBody TestDTO dto) {
        Test domain = testDtoMapping.toDomain(dto);
        domain.setId(id);
        Test rt = testService.prepareParam(domain);
        return ResponseEntity.status(HttpStatus.OK).body(testDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 测试实体
    * 
    *
    * @param dto dto
    * @return ResponseEntity<TestDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"测试实体" },  notes = "Test-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-Save-all') or hasPermission(this.testDtoMapping.toDomain(#dto),'uaa-Test-Save')")
    @PostMapping("tests/save")
    public ResponseEntity<TestDTO> save
            (@Validated @RequestBody TestDTO dto) {
        Test domain = testDtoMapping.toDomain(dto);
        testService.save(domain);
        Test rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(testDtoMapping.toDto(rt));
    }

    /**
    * User 测试实体
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<TestDTO>
    */
    @ApiOperation(value = "User", tags = {"测试实体" },  notes = "Test-User ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-User-all') or hasPermission(this.testDtoMapping.toDomain(#dto),'uaa-Test-User')")
    @PostMapping("tests/{id}/user")
    public ResponseEntity<TestDTO> userById
            (@PathVariable("id") String id, @Validated @RequestBody TestDTO dto) {
        Test domain = testDtoMapping.toDomain(dto);
        domain.setId(id);
        Test rt = testService.user(domain);
        return ResponseEntity.status(HttpStatus.OK).body(testDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault 测试实体
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<TestDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"测试实体" },  notes = "Test-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-FetchDefault-all') or hasPermission(#dto,'uaa-Test-FetchDefault')")
    @PostMapping("tests/fetchdefault")
    public ResponseEntity<List<TestDTO>> fetchDefault
            (@Validated @RequestBody TestFilterDTO dto) {
        TestSearchContext context = testFilterDtoMapping.toDomain(dto);
        Page<Test> domains = testService.searchDefault(context) ;
        List<TestDTO> list = testDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建测试实体
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-Create-all')")
    @ApiOperation(value = "批量新建测试实体", tags = {"测试实体" },  notes = "批量新建测试实体")
	@PostMapping("tests/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<TestDTO> dtos) {
        testService.createBatch(testDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除测试实体
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-Remove-all')")
    @ApiOperation(value = "批量删除测试实体", tags = {"测试实体" },  notes = "批量删除测试实体")
	@DeleteMapping("tests/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        testService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新测试实体
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-Update-all')")
    @ApiOperation(value = "批量更新测试实体", tags = {"测试实体" },  notes = "批量更新测试实体")
	@PutMapping("tests/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<TestDTO> dtos) {
        testService.updateBatch(testDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存测试实体
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-Save-all')")
    @ApiOperation(value = "批量保存测试实体", tags = {"测试实体" },  notes = "批量保存测试实体")
	@PostMapping("tests/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<TestDTO> dtos) {
        testService.saveBatch(testDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入测试实体
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-Test-Save-all')")
    @ApiOperation(value = "批量导入测试实体", tags = {"测试实体" },  notes = "批量导入测试实体")
	@PostMapping("tests/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<TestDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(testService.importData(config,ignoreError,testDtoMapping.toDomain(dtos)));
    }

}
